package com.znsd.zl.house.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.znsd.zl.house.dao.RepairDao;
import com.znsd.zl.house.entity.RepairEntity;
import com.znsd.zl.house.entity.RepairImageEntity;
import com.znsd.zl.house.entity.vo.RepairRespVO;
import com.znsd.zl.house.service.IRepairService;
import com.znsd.zl.house.utils.ConditionPageUtils;
import com.znsd.zl.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName RepairServiceImpl
 * @Description 描述
 * @author zhubin
 * @date 2020-08-21 10:49:21
 * @Version v1.0
 */
@Slf4j
@Service
public class RepairServiceImpl extends ServiceImpl<RepairDao, RepairEntity> implements IRepairService {

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		// 使用工具类获取map的 当前页和展示多少天
		int[] pageAndLimit = ConditionPageUtils.getPageAndLimit(params);
		int currentPage = pageAndLimit[0];
		int size = pageAndLimit[1];
		// 计算从那开始
		Integer pageNum = (currentPage -1) * size;
		List<RepairRespVO> repairs = this.baseMapper.queryPage(params, pageNum, size);
		log.info("报修数据====="+repairs);
		// 封装分页数据
		return ConditionPageUtils.pageData(currentPage,size,repairs,queryCount(params));
	}

	@Override
	public Integer queryCount( Map<String, Object> params) {
		return this.baseMapper.queryCount(params);
	}

	@Override
	public List<String> findRepairImage(Integer repairId) {
		return this.baseMapper.findRepairImage(repairId);
	}


	@Override
	public void solve(Integer id) {
		RepairEntity repairEntity = this.baseMapper.selectById(id);
		if (repairEntity != null) {
			repairEntity.setRepairStatus(1);
			this.baseMapper.updateById(repairEntity);
		}
	}

	@Override
	public boolean saveRepairImage(RepairImageEntity imageEntity) {
		if (imageEntity!=null){
			baseMapper.saveRepairImage(imageEntity);
			return true;
		}
		return false;
	}

	@Override
	public boolean saveRepair(RepairEntity repairEntity,List<String> imgList) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = df.format(new Date());
		if (repairEntity!=null){
			repairEntity.setCreateTime(format);
			int insert = baseMapper.insert(repairEntity);
			if (imgList!=null){
				String strip = StringUtils.strip(imgList.toString(), "[]");
				String[] split = strip.split(",");
				for (int i = 0; i <split.length ; i++) {
					int count=1;
					if (i > 0) {
						count=count+1;
					}
					String str=split[i].substring(count,split[i].length()-1);
					RepairImageEntity imageEntity = new RepairImageEntity();
					imageEntity.setUrl(str);
					imageEntity.setRepairId(repairEntity.getId());
					baseMapper.saveRepairImage(imageEntity);
				}
			}
			return true;
		}

		return false;
	}

	/**
	 * 删除和批量逻辑删除
	 * @param idList  id
	 */
	@Override
	public void deleteBatch( List<Integer> idList){
		this.baseMapper.deleteBatch(idList);
		/**
		 * 删除维修表关联的图片
		 */
		for (Integer id:idList){
			baseMapper.deleteRepairImage(id);
		}
	}

	@Override
	public void deleteRepairImage(Integer repairId) {
		baseMapper.deleteById(repairId);
		baseMapper.deleteRepairImage(repairId);
	}

}
